# Proja - project management software
# Copyright (C) 2012  Oleg Kasian
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'erb'
require 'ostruct'
require 'application_helper'

class ErbLoaderImpl < OpenStruct

    include ApplicationHelper

    def resolveView(out, s, model)
        out.write(ErbLoaderImpl.new(model).render_template(s))
    end

    def render_template(s)
        ERB.new(File.read(s)).result(binding)
    end

    def setApplicationContext(ac)
        @@ac = ac
    end

    def setServletContext(sc)
        @@sc = sc
    end

    def get_bean name
        @@ac.getBean(name)
    end
    
    def resolve_path path
      resolver = get_bean "viewResolver"
      path = resolver.getViewPrefix + path + resolver.getSuffix
      @@sc.getRealPath(path)
    end
end

ErbLoaderImpl.new